﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace UltimateFracturing
{
	// Token: 0x0200043B RID: 1083
	public class MeshFaceConnectivity
	{
		// Token: 0x06001E61 RID: 7777 RVA: 0x000C19F8 File Offset: 0x000BFBF8
		public MeshFaceConnectivity()
		{
			this.listTriangles = new List<MeshFaceConnectivity.TriangleData>();
			this.listEdges = new List<MeshFaceConnectivity.EdgeData>();
			this.listEdgeIndices = new List<int>();
			this.dicEdges = new Dictionary<EdgeKeyByHash, MeshFaceConnectivity.EdgeData>(new EdgeKeyByHash.EqualityComparer());
			this.dicSubMeshTriangleCount = new Dictionary<int, int>();
			this.nEdgeCount = 0;
		}

		// Token: 0x06001E62 RID: 7778 RVA: 0x000C1A4E File Offset: 0x000BFC4E
		public void Clear()
		{
			this.listTriangles.Clear();
			this.listEdges.Clear();
			this.listEdgeIndices.Clear();
			this.dicEdges.Clear();
			this.dicSubMeshTriangleCount.Clear();
			this.nEdgeCount = 0;
		}

		// Token: 0x06001E63 RID: 7779 RVA: 0x000C1A90 File Offset: 0x000BFC90
		public void ResetVisited()
		{
			for (int i = 0; i < this.listTriangles.Count; i++)
			{
				this.listTriangles[i].bVisited = false;
			}
		}

		// Token: 0x06001E64 RID: 7780 RVA: 0x000C1ACC File Offset: 0x000BFCCC
		public void AddEdge(int nSubMesh, Vector3 v1, Vector3 v2, int nVertex1Hash, int nVertex2Hash, int nVertexDataIndex1, int nVertexDataIndex2)
		{
			int nEdgePos = this.nEdgeCount % 3;
			int num = this.nEdgeCount / 3;
			if (!this.dicSubMeshTriangleCount.ContainsKey(nSubMesh))
			{
				this.dicSubMeshTriangleCount.Add(nSubMesh, 0);
			}
			int num2 = this.dicSubMeshTriangleCount[nSubMesh];
			EdgeKeyByHash key = new EdgeKeyByHash(nVertex1Hash, nVertex2Hash);
			int num3;
			if (this.dicEdges.ContainsKey(key))
			{
				MeshFaceConnectivity.EdgeData edgeData = this.dicEdges[key];
				edgeData.AddSideData(num, nSubMesh, num2, nEdgePos, nVertexDataIndex1, nVertexDataIndex2);
				num3 = edgeData.nEdgeIndex;
			}
			else
			{
				num3 = this.listEdges.Count;
				MeshFaceConnectivity.EdgeData edgeData2 = new MeshFaceConnectivity.EdgeData(num3, num, nSubMesh, num2, nEdgePos, v1, v2, nVertex1Hash, nVertex2Hash, nVertexDataIndex1, nVertexDataIndex2);
				this.listEdges.Add(edgeData2);
				this.dicEdges.Add(key, edgeData2);
			}
			this.listEdgeIndices.Add(num3);
			this.nEdgeCount++;
			if (this.nEdgeCount % 3 == 0)
			{
				MeshFaceConnectivity.TriangleData triangleData = new MeshFaceConnectivity.TriangleData(nSubMesh, num2);
				MeshFaceConnectivity.EdgeData edgeData3 = this.listEdges[this.listEdgeIndices[this.listEdgeIndices.Count - 3]];
				MeshFaceConnectivity.EdgeData edgeData4 = this.listEdges[this.listEdgeIndices[this.listEdgeIndices.Count - 2]];
				MeshFaceConnectivity.EdgeData edgeData5 = this.listEdges[this.listEdgeIndices[this.listEdgeIndices.Count - 1]];
				triangleData.anEdges[0] = edgeData3.nEdgeIndex;
				triangleData.anEdges[1] = edgeData4.nEdgeIndex;
				triangleData.anEdges[2] = edgeData5.nEdgeIndex;
				foreach (MeshFaceConnectivity.EdgeData.SideData sideData in edgeData3.listSides)
				{
					if (sideData.nFace != num)
					{
						this.listTriangles[sideData.nFace].alistNeighborSubMeshes[sideData.nEdgePos].Add(nSubMesh);
						this.listTriangles[sideData.nFace].alistNeighborTriangles[sideData.nEdgePos].Add(num2);
						triangleData.alistNeighborSubMeshes[0].Add(sideData.nSubMesh);
						triangleData.alistNeighborTriangles[0].Add(sideData.nSubMeshFace);
					}
				}
				foreach (MeshFaceConnectivity.EdgeData.SideData sideData2 in edgeData4.listSides)
				{
					if (sideData2.nFace != num)
					{
						this.listTriangles[sideData2.nFace].alistNeighborSubMeshes[sideData2.nEdgePos].Add(nSubMesh);
						this.listTriangles[sideData2.nFace].alistNeighborTriangles[sideData2.nEdgePos].Add(num2);
						triangleData.alistNeighborSubMeshes[1].Add(sideData2.nSubMesh);
						triangleData.alistNeighborTriangles[1].Add(sideData2.nSubMeshFace);
					}
				}
				foreach (MeshFaceConnectivity.EdgeData.SideData sideData3 in edgeData5.listSides)
				{
					if (sideData3.nFace != num)
					{
						this.listTriangles[sideData3.nFace].alistNeighborSubMeshes[sideData3.nEdgePos].Add(nSubMesh);
						this.listTriangles[sideData3.nFace].alistNeighborTriangles[sideData3.nEdgePos].Add(num2);
						triangleData.alistNeighborSubMeshes[2].Add(sideData3.nSubMesh);
						triangleData.alistNeighborTriangles[2].Add(sideData3.nSubMeshFace);
					}
				}
				this.listTriangles.Add(triangleData);
				Dictionary<int, int> dictionary;
				(dictionary = this.dicSubMeshTriangleCount)[nSubMesh] = dictionary[nSubMesh] + 1;
			}
		}

		// Token: 0x04001ED6 RID: 7894
		public List<MeshFaceConnectivity.TriangleData> listTriangles;

		// Token: 0x04001ED7 RID: 7895
		private List<MeshFaceConnectivity.EdgeData> listEdges;

		// Token: 0x04001ED8 RID: 7896
		private List<int> listEdgeIndices;

		// Token: 0x04001ED9 RID: 7897
		private Dictionary<EdgeKeyByHash, MeshFaceConnectivity.EdgeData> dicEdges;

		// Token: 0x04001EDA RID: 7898
		private int nEdgeCount;

		// Token: 0x04001EDB RID: 7899
		private Dictionary<int, int> dicSubMeshTriangleCount;

		// Token: 0x0200043C RID: 1084
		public struct EdgeData
		{
			// Token: 0x06001E65 RID: 7781 RVA: 0x000C1EF4 File Offset: 0x000C00F4
			public EdgeData(int nEdgeIndex, int nFace, int nSubMesh, int nSubMeshFace, int nEdgePos, Vector3 v1, Vector3 v2, int nVertex1Hash, int nVertex2Hash, int nVertexDataV1, int nVertexDataV2)
			{
				this.nEdgeIndex = nEdgeIndex;
				this.v1 = v1;
				this.v2 = v2;
				this.nVertex1Hash = nVertex1Hash;
				this.nVertex2Hash = nVertex2Hash;
				this.listSides = new List<MeshFaceConnectivity.EdgeData.SideData>();
				this.listSides.Add(new MeshFaceConnectivity.EdgeData.SideData(nFace, nSubMesh, nSubMeshFace, nEdgePos, nVertexDataV1, nVertexDataV2));
			}

			// Token: 0x06001E66 RID: 7782 RVA: 0x000C1F4D File Offset: 0x000C014D
			public bool Compare(int nVertex1Hash, int nVertex2Hash)
			{
				return (this.nVertex1Hash == nVertex1Hash && this.nVertex2Hash == nVertex2Hash) || (this.nVertex1Hash == nVertex2Hash && this.nVertex2Hash == nVertex1Hash);
			}

			// Token: 0x06001E67 RID: 7783 RVA: 0x000C1F84 File Offset: 0x000C0184
			public void AddSideData(int nFace, int nSubMesh, int nSubMeshFace, int nEdgePos, int nVertexDataV1, int nVertexDataV2)
			{
				if (this.listSides == null)
				{
					this.listSides = new List<MeshFaceConnectivity.EdgeData.SideData>();
				}
				this.listSides.Add(new MeshFaceConnectivity.EdgeData.SideData(nFace, nSubMesh, nSubMeshFace, nEdgePos, nVertexDataV1, nVertexDataV2));
			}

			// Token: 0x06001E68 RID: 7784 RVA: 0x000C1FB5 File Offset: 0x000C01B5
			public bool HasMoreThanOneSide()
			{
				return this.listSides != null && this.listSides.Count > 1;
			}

			// Token: 0x04001EDC RID: 7900
			public int nEdgeIndex;

			// Token: 0x04001EDD RID: 7901
			private int nVertex1Hash;

			// Token: 0x04001EDE RID: 7902
			private int nVertex2Hash;

			// Token: 0x04001EDF RID: 7903
			public Vector3 v1;

			// Token: 0x04001EE0 RID: 7904
			public Vector3 v2;

			// Token: 0x04001EE1 RID: 7905
			public List<MeshFaceConnectivity.EdgeData.SideData> listSides;

			// Token: 0x0200043D RID: 1085
			public struct SideData
			{
				// Token: 0x06001E69 RID: 7785 RVA: 0x000C1FD2 File Offset: 0x000C01D2
				public SideData(int nFace, int nSubMesh, int nSubMeshFace, int nEdgePos, int nVertexDataV1, int nVertexDataV2)
				{
					this.nFace = nFace;
					this.nSubMesh = nSubMesh;
					this.nSubMeshFace = nSubMeshFace;
					this.nEdgePos = nEdgePos;
					this.nVertexDataV1 = nVertexDataV1;
					this.nVertexDataV2 = nVertexDataV2;
				}

				// Token: 0x04001EE2 RID: 7906
				public int nFace;

				// Token: 0x04001EE3 RID: 7907
				public int nSubMesh;

				// Token: 0x04001EE4 RID: 7908
				public int nSubMeshFace;

				// Token: 0x04001EE5 RID: 7909
				public int nEdgePos;

				// Token: 0x04001EE6 RID: 7910
				public int nVertexDataV1;

				// Token: 0x04001EE7 RID: 7911
				public int nVertexDataV2;
			}
		}

		// Token: 0x0200043E RID: 1086
		public class TriangleData
		{
			// Token: 0x06001E6A RID: 7786 RVA: 0x000C2004 File Offset: 0x000C0204
			public TriangleData(int nSubMesh, int nTriangle)
			{
				this.nSubMesh = nSubMesh;
				this.nTriangle = nTriangle;
				this.anEdges = new int[3];
				this.alistNeighborSubMeshes = new List<int>[3];
				this.alistNeighborTriangles = new List<int>[3];
				for (int i = 0; i < 3; i++)
				{
					this.anEdges[i] = -1;
					this.alistNeighborSubMeshes[i] = new List<int>();
					this.alistNeighborTriangles[i] = new List<int>();
				}
				this.bVisited = false;
			}

			// Token: 0x04001EE8 RID: 7912
			public int nSubMesh;

			// Token: 0x04001EE9 RID: 7913
			public int nTriangle;

			// Token: 0x04001EEA RID: 7914
			public int[] anEdges;

			// Token: 0x04001EEB RID: 7915
			public List<int>[] alistNeighborSubMeshes;

			// Token: 0x04001EEC RID: 7916
			public List<int>[] alistNeighborTriangles;

			// Token: 0x04001EED RID: 7917
			public bool bVisited;
		}
	}
}
